package com.zut.base.controller;

import com.zut.base.common.entry.ResultVO;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 静态资源处理
 */
@RestController
@RequestMapping("/static/report")
public class ResourceController {

    private static String path ="G://img/";

    //处理图片 (待优化)
    @PostMapping("/sendImg")
    public Object sendImg(@Param("file") MultipartFile file){
        if(file == null) return new ResultVO<>(-9999,"上传失败！");
        Map map = new HashMap<>();
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID().toString().replaceAll("-","") + suffixName; // 新文件名
        File dest = new File(path + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("code",200);
        map.put("path","http://localhost:8888/img/" + fileName);
        return new ResultVO<>(map);
    }
}
